package com.time.client.application.controller;

import com.time.auth.entity.Result;
import com.time.client.application.converter.TagDTOConverter;
import com.time.client.application.dto.TagDTO;
import com.time.client.common.entity.PageResult;
import com.time.client.domain.entity.TagBO;
import com.time.client.domain.service.ClientTagDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 标签展示控制层类
 *
 * @author: HuangQi
 * @date: Created in 11:09 2025/10/14
 * @description: 标签展示控制层
 */
@Slf4j
@RestController
@RequestMapping("/client/tag/")
public class TagController {

    @Autowired
    private ClientTagDomainService clientTagDomainService;

    /**
     * 分页查询所有标签
     *
     * @param tagDTO 分页查询所有标签信息
     * @return 分页标签信息结果
     */
    @PostMapping("queryAll")
    public Result<PageResult<TagDTO>> queryAllTag(@RequestBody TagDTO tagDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Client.TagController.queryAllTag.dto: {}", tagDTO);
            }

            // 调用领域层
            TagBO tagBO = TagDTOConverter.INSTANCE.convertDTOToBO(tagDTO);
            PageResult<TagBO> infoBOPageResult = clientTagDomainService.queryAllTag(tagBO);

            // 返回结果
            PageResult<TagDTO> infoDTOPageResult = TagDTOConverter.INSTANCE.convertBOPageToDTOPage(infoBOPageResult);
            return Result.ok(infoDTOPageResult);
        } catch (Exception e) {
            // 异常处理
            log.error("Client.TagController.queryAllTag.error:{}", e.getMessage(), e);
            return Result.fail("展示模块分页获取所有标签失败");
        }
    }

    /**
     * 获取排名前10的热门标签
     *
     * @return 标签信息列表
     */
    @PostMapping("queryHotTag")
    public Result<List<TagDTO>> queryHotTag() {
        try {
            if (log.isInfoEnabled()) {
                log.info("Client.TagController.queryHotTag");
            }

            // 调用领域层
            List<TagBO> tagBOList = clientTagDomainService.queryHotTag();

            // 返回结果
            List<TagDTO> tagDTOList = TagDTOConverter.INSTANCE.convertBOListToDTO(tagBOList);
            return Result.ok(tagDTOList);
        } catch (Exception e) {
            // 异常处理
            log.error("Client.TagController.queryHotTag.error:{}", e.getMessage(), e);
            return Result.fail("展示模块获取热门标签失败");
        }
    }

}
